<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 命名捕获
    // (?<名字>)
    {
      let str = "2018-03-20";
      let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
      let {
        year,
        month,
        day
      } = str.match(reg).groups;
      console.log(year, month, day);
    }; {
      // 反向应用命名捕获
      // \k<名字>
      let reg1 = /^(?<Strive>weclome)-\k<Strive>$/;
      let str1 = "a-a";
      let str2 = "Strive-Strive";
      let str3 = "weclome-weclome";
      console.log(reg1.test(str1));
      console.log(reg1.test(str2));
      console.log(reg1.test(str3));

      let reg2 = /^(?<strive>admin)-\k<strive>-\1$/;
      // \1  \2 \3
      let str4 = "admin-admin-admin";
      console.log(reg2.test(str4));
    }; {
      //与replace 结合起来 替换
      let str = "2019-05-17";
      let reg = /(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/;
      let str1 = str.replace(reg, "$<y>/$<m>/$<d>");
      console.log(str1);
    }; {
      // replace 与 回调函数结合使用
      let str = "2020-01-20";
      let reg = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
      let str1 = str.replace(reg, (...arg) => {
        console.log(...arg);
        let {
          year,
          month,
          day
        } = arg[arg.length - 1];
        return `${year}/${month}/${day}`;
      });
      console.log(str1);
    }
  </script>
</body>

</html>